package com.example.server.controller;

import com.example.server.entity.Coupon;
import com.example.server.redis.RedisService;
import com.example.server.service.CouponService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("coupon")
public class CouponController extends BaseController{
   private final CouponService couponService;
   private final RedisService redisService;

    public CouponController(CouponService couponService, RedisService redisService) {
        this.couponService = couponService;
        this.redisService = redisService;
    }

    @PostMapping("create")
    private int create(@RequestBody Coupon coupon){
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        Coupon coupon1 = new Coupon();
        coupon1.setUid(uid);
        coupon1.setCode(coupon.getCode());
        coupon1.setAmount(coupon.getAmount());
        Coupon coupon2 = couponService.queryOne("queryQuantity",coupon1);
        if(coupon2 == null){
            return couponService.create(coupon1);
        }else{
            int quantity = coupon2.getQuantity();
            int num = quantity + 1;
            coupon1.setQuantity(num);
            return couponService.modifyOne("modifyQuantity",coupon1);
        }
    }


    @GetMapping("queryThirty")
    private Coupon queryThirty(){
        Coupon coupon = new Coupon();
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        coupon.setUid(uid);
        coupon.setCode("满300减30");
        return couponService.queryOne("queryThirty",coupon);
    }

    @GetMapping("queryFifty")
    private Coupon queryFifty(){
        Coupon coupon = new Coupon();
        int uid = Integer.parseInt(redisService.getSetString("uid"));
        coupon.setUid(uid);
        coupon.setCode("满500减50");
        return couponService.queryOne("queryFifty",coupon);
    }
}
